/*
    XTU OJ: 1239
    by: fifth_light
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int numbers[4][4];
char command[8];

typedef enum {
    UP, DOWN, LEFT, RIGHT
} direction;

void action() {
    direction dir;
    if (strcmp("UP", command) == 0) {
        dir = UP;
    } else if (strcmp("DOWN", command) == 0) {
        dir = DOWN;
    } else if (strcmp("LEFT", command) == 0) {
        dir = LEFT;
    } else if (strcmp("RIGHT", command) == 0) {
        dir = RIGHT;
    } else {
        exit(-1);
    }
    switch (dir) {
        case LEFT:
            for (int y = 0; y < 4; y++) {
                int flags[4] = {0};
                for (int x = 1; x < 4; x++) {
                    if (numbers[y][x] != 0) {
                        // 向左回溯
                        for (int i = x - 1; i >= 0; i--) {
                            int reached = numbers[y][i];
                            if (reached == 0 && i != 0) {
                                continue;
                            }
                            if (reached == numbers[y][x] && flags[i] == 0) {
                                numbers[y][x] = 0;
                                numbers[y][i] *= 2;
                                flags[i] = 1;
                            } else {
                                if ((i == 0 && numbers[y][0] == 0)) {
                                    numbers[y][0] = numbers[y][x];
                                    numbers[y][x] = 0;
                                } else if (numbers[y][i + 1] == 0) {
                                    numbers[y][i + 1] = numbers[y][x];
                                    numbers[y][x] = 0;
                                }
                            }
                            break;
                        }
                    }
                }
            }
            break;
        case RIGHT:
            for (int y = 0; y < 4; y++) {
                int flags[4] = {0};
                for (int x = 2; x >= 0; x--) {
                    if (numbers[y][x] != 0) {
                        // 向右回溯
                        for (int i = x + 1; i < 4; i++) {
                            int reached = numbers[y][i];
                            if (reached == 0 && i != 3) {
                                continue;
                            }
                            if (reached == numbers[y][x] && flags[i] == 0) {
                                numbers[y][x] = 0;
                                numbers[y][i] *= 2;
                                flags[i] = 1;
                            } else {
                                if ((i == 3 && numbers[y][3] == 0)) {
                                    numbers[y][3] = numbers[y][x];
                                    numbers[y][x] = 0;
                                } else if (numbers[y][i - 1] == 0) {
                                    numbers[y][i - 1] = numbers[y][x];
                                    numbers[y][x] = 0;
                                }
                            }
                            break;
                        }
                    }
                }
            }
            break;
        case UP:
            for (int x = 0; x < 4; x++) {
                int flags[4] = {0};
                for (int y = 1; y < 4; y++) {
                    if (numbers[y][x] != 0) {
                        // 向下回溯
                        for (int i = y - 1; i >= 0; i--) {
                            int reached = numbers[i][x];
                            if (reached == 0 && i != 0) {
                                continue;
                            }
                            if (reached == numbers[y][x] && flags[i] == 0) {
                                numbers[y][x] = 0;
                                numbers[i][x] *= 2;
                                flags[i] = 1;
                            } else {
                                if ((i == 0 && numbers[0][x] == 0)) {
                                    numbers[0][x] = numbers[y][x];
                                    numbers[y][x] = 0;
                                } else if (numbers[i + 1][x] == 0) {
                                    numbers[i + 1][x] = numbers[y][x];
                                    numbers[y][x] = 0;
                                }
                            }
                            break;
                        }
                    }
                }
            }
            break;
        case DOWN:
            for (int x = 0; x < 4; x++) {
                int flags[4] = {0};
                for (int y = 2; y >= 0; y--) {
                    if (numbers[y][x] != 0) {
                        // 向下回溯
                        for (int i = y + 1; i < 4; i++) {
                            int reached = numbers[i][x];
                            if (reached == 0 && i != 3) {
                                continue;
                            }
                            if (reached == numbers[y][x] && flags[i] == 0) {
                                numbers[y][x] = 0;
                                numbers[i][x] *= 2;
                                flags[i] = 1;
                            } else {
                                if ((i == 3 && numbers[3][x] == 0)) {
                                    numbers[3][x] = numbers[y][x];
                                    numbers[y][x] = 0;
                                } else if (numbers[i - 1][x] == 0) {
                                    numbers[i - 1][x] = numbers[y][x];
                                    numbers[y][x] = 0;
                                }
                            }
                            break;
                        }
                    }
                }
            }
            break;
    }
    for (int y = 0; y < 4; y++) {
        for (int x = 0; x < 4; x++) {
            printf(x < 3 ? "%d " : "%d", numbers[y][x]);
        }
        putchar('\n');
    }
    putchar('\n');
}

int main(void) {
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; i++) {
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                scanf("%d", &numbers[y][x]);
            }
        }
        scanf("%s", command);
        action();
    }
    return 0;
}
